﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 根据品种编号搜索所有需求，并自动进行累计
    /// 选择进入需要创建采购单或生产单队列，
    /// 创建生产单
    /// 创建留位关系
    /// </summary>
    public partial class TPWD02
    {
      

        #region ==VB===========
        public static readonly DependencyProperty VBProperty = DependencyProperty.Register("VB",
        typeof(string), typeof(TPWD02),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVBChanged));
        public string VB
        {
            get
            {
                return (string)GetValue(VBProperty);
            }
            set
            {
                SetValue(VBProperty, value);
            }
        }
        private static void OnVBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPWD02;
            if (src != null)
            {
                src.VB = (string)e.NewValue;
            }
        }
        #endregion

       
        public TPWD02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWDECK();
            DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        public override void OnKDBLOCKChg(bool value)
        {


        }
        public override void OnVKBLOCKChg(string value)
        {
            if (value.Length < 9)
                return;
            vwMaster.View.MoveCurrentTo(-1);
            filter = "WDSA='" + value.Substring(0, 1) + "' AND WDAUFKRS='" + value.Substring(1, 2) + "' AND WDAUFLFD>='" + value.Substring(3, 6) + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.OnVKBLOCKChg(value);
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.Clone().NewRow();
                dwData["WDMANDANT"] = app.Mandant;
                dwData["WDSA"] = AUFART;
                dwData["WDTYP"] = "B";
                dwData["WDART"] = ART.Trim();
                dwData["WDFARBE"] = FARBE.Trim();
                //dwData["WDBEDST"] = " AND VKKVB='" + VB + "'";
                dwData["WDAUFKRS"] = " AND WDAUFKRS LIKE '%" + KRS.Trim() + "%'";
                dwData["WDAUFLFD"] = " AND WDAUFLFD LIKE '%" + LFD.Trim() + "%'";
                dwData["WDAUFPOS"] = " AND WDAUFPOS LIKE '%" + POS.Trim() + "%'";
                SResult = TMV.QWD25(dwData);
                MergeData(SResult, false);
                IsVKPaid = !IsVKPaid;
                IsVKPaid = !IsVKPaid;
                base.QueryCommand(sender, e);
                TBDetail.Clear();
                TBDetail.Merge(TBMain.Clone());
                vwDetail.Source = TBDetail;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnPaidOnly(bool value)
        {
            if (!IsLoaded)
                return;
            if (value)
            {
                cvMaster.CustomFilter = "VKPBSTSTA='70' ";
            }
            else
            {
                cvMaster.CustomFilter = null;
            }
            base.OnPaidOnly(value);
        }

    

        private void AllToPurchaseCommand(object sender, RoutedEventArgs e)
        {

        }

        

        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            KRS = MV.year.Substring(2, 2);
            switch (app.company)
            {
                case "TST":
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            // KRS = "S" + MV.year.Substring(3, 1);
                            VB = "TSK";
                            break;
                        case "TMODE":
                            // KRS = "M" + MV.year.Substring(3, 1);
                            VB = "TMK";
                            break;

                    }
                    break;
            }
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length == 0)
                return;
            AUFART = ORDERNUM.Substring(0, 1);
            KRS = ORDERNUM.Substring(1, 2);
            LFD = ORDERNUM.Substring(3, 6);
            if (IsLoaded)
                return;
            base.LOADBYAUF();
            if (!IsAuto)
                return;

        }
        public override void OnVKKChg(string vkk)
        {
            if (!IsAuto)
                return;
            if (vkk.Length == 0)
                return;
            AUFART = vkk.Substring(0, 1);
            KRS = vkk.Substring(1, 2);
            LFD = vkk.Substring(3, 6);
            PM["FKRS"] = KRS;
            string WDTYP = "B";
            if (AUFART == "K")
            {
                WDTYP = "B";
            }
            dwData = TBMain.NewRow();
            dwData["WDMANDANT"] = app.Mandant;
            dwData["WDSA"] = AUFART;
            dwData["WDTYP"] = WDTYP;
            dwData["WDART"] = ART.Trim();
            dwData["WDFARBE"] = FARBE.Trim();
            //dwData["WDBEDST"] = " AND VKKVB='" + VB + "'";
            dwData["WDAUFKRS"] = " AND WDAUFKRS LIKE '" + vkk.Substring(1, 2) + "%'";
            dwData["WDAUFLFD"] = " AND WDAUFLFD LIKE '" + vkk.Substring(3, 6) + "%'";
            dwData["WDAUFPOS"] = " AND WDAUFPOS LIKE '" + POS + "%'";
            SResult = TMV.QWD25(dwData);
            MergeData(SResult, false, true);
           
            WD2EK();
        }
        public void WD2EK()
        {
            if (TBMain.Rows.Count == 0)
                return;
            DataSet Set = new DataSet();
            if (LIEFNR != "")
            {
                PM["LSLIEFNR"] = LIEFNR;
                foreach(DataRow row in TBMain.Rows)
                {
                    row["LSLIEFNR"] = LIEFNR;
                }
            }
            if (IsVKPaid)
            {
                filter = "VKPBSTSTA='70' ";
                rows = TBMain.Select(filter);
                Set.Merge(rows.CopyToDataTable());
            }
            else
            {
                Set.Merge(TBMain);
            } 
            PM["LSLIEFNR"] = TBMain.Rows[0]["LSLIEFNR"].ToString();
            
            PM["FKRS"] = "E" + MV.year.Substring(3, 1);
            PM["WAEHRSL"] = TBMain.Rows[0]["WAEHRSL"].ToString();
            PM["IsVKPaid"] = IsVKPaid;
            PM["Set"] = Set;
            EKAUF = "";
            EKAUF = TMV.VEK.EKAUTO(PM);
            CLSLIEFNR = "";
        }
       
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (CLSLIEFNR == "")
            {
                CLSLIEFNR = dwMaster["LSLIEFNR"].ToString();
            }
            else
            {
                if (CLSLIEFNR != dwMaster["LSLIEFNR"].ToString())
                {
                    MessageBox.Show(CLSLIEFNR+ "供应商不同!");
                    return;
                }               
            }
              
            filter = "WDAUFKRS='" + dwMaster["WDAUFKRS"].ToString() + "' AND WDAUFLFD='" + dwMaster["WDAUFLFD"].ToString() + "' AND WDAUFPOS='" + dwMaster["WDAUFPOS"].ToString() + "'";
            rows =TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                dwData = TBDetail.NewRow();
                dwData.ItemArray = row.ItemArray;
                TBDetail.Rows.InsertAt(dwData, 0);
                row.Delete();
            }
            TBMain.AcceptChanges(); 
            TBDetail.AcceptChanges(); 
        }

        public void ByLSCommand(object sender, RoutedEventArgs e)
        {
            if (dwWD == null)
                return;
            DataSet Set = new DataSet();
            if (IsVKPaid)
            {
                filter = "LSLIEFNR='" + dwWD["LSLIEFNR"].ToString() + "' AND VKPBSTSTA='70'";

            }
            else
            {
                filter = "LSLIEFNR='" + dwWD["LSLIEFNR"].ToString() + "'";
                Set.Merge(TBMain);
            }

            rows = TBMain.Select(filter);
            if (rows.Length == 0)
                return;
            DataTable tb = rows.CopyToDataTable();
            TBDetail.Merge(tb); 
            vwMaster.View.MoveCurrentTo(-1); 
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwDetail == null)
                return;
            filter = "WDAUFKRS='"+dwDetail["WDAUFKRS"].ToString()+ "' AND WDAUFLFD='"+dwDetail["WDAUFLFD"].ToString()+ "' AND WDAUFPOS='"+dwDetail["WDAUFPOS"].ToString()+"'"                ;
            rows = TBDetail.Select(filter);
            vwDetail.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                TBMain.Rows.InsertAt(dwData, 0);
                row.Delete();
            }
            TBMain.AcceptChanges();
            TBDetail.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        
    }
}
